﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.BF.Ldap;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Client;
using Sio.Mdm.Types;

namespace Sio.Mdm.Web.Instalacija
{
    public partial class LdapPravilaSeznam : Controls.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                LoadLdapTipi();
                NaloziSeznamGrida(e);
            }
        }

        #region Private
        private void NaloziSeznamGrida(EventArgs arg)
        {                        
            GridPravilaSeznam.LoadData<LdapPraviloRazsirjeno>(
                new Filter<LdapPraviloRazsirjeno>().Where(r => r.LdapPraviloID != null).OrderBy(r => r.Ime));                        
        }

        private void LoadLdapTipi()
        {
            Dictionary<int, string> listTipi = new Dictionary<int, string>();
            listTipi.Add(0, "AD");
            listTipi.Add(1, "LDAP");
            var tmpArray = (from p in listTipi select new Object[2] { p.Key, p.Value }).ToArray();
            
            StoreLdapTipi.DataSource = tmpArray;
            StoreLdapTipi.DataBind();
        }

        #endregion

        #region Protected
        #region TestiranjePravila
        protected void BrisanjePoPravilu(object sender, DirectEventArgs e)
        {
            List<int> selectedItems = e.GetIdList<int>("selected", "LdapPraviloID");
            if (selectedItems.Count != 1)
            {
                ShowExtMessageBox("Opozorilo", "Prosimo izberite natanko eno pravilo!");
                return;
            }

            var slServiceLdap = ServiceFactory.GetService<ILdapService>();
            Result result = slServiceLdap.UserRequestDeletePravilo(selectedItems[0], UserSession);

            if (result != null)
            {
                string error = string.Format("<b>Prišlo je do napake pri brisanju podatkov!</b><br><br>Opis: {0}", result.Message);
                ShowExtMessageBox("Napaka", error);
                return;
            }
            ShowExtNotification("LDAP/AD Brisanje", "Brisanje podatkov po pravilu se je uspešno izvršilo.");
        }
        #endregion

        #region Synchronize
        protected void SinhronizacijaPoPravilu(object sender, DirectEventArgs e)
        {            
            List<int> selectedItems = e.GetIdList<int>("selected", "LdapPraviloID");
            if (selectedItems.Count != 1) {
                ShowExtMessageBox("Opozorilo", "Prosimo izberite natanko eno pravilo!");
                return;
            }

            var slServiceLdap = ServiceFactory.GetService<ILdapService>();
            Result result = slServiceLdap.UserRequestSyncPravilo(selectedItems[0], UserSession);

            if (result != null)
            {
                string error = string.Format("<b>Prišlo je do napake pri sinhronizaciji podatkov!</b><br><br>Opis: {0}", result.Message);
                ShowExtMessageBox("Napaka", error);
                return;
            }
            ShowExtNotification("LDAP/AD Sinhronizacija", "Sinhronizacija pravila se je uspešno izvršila.");
        }
        #endregion

        #region PreveriPripadnost
        protected void PreveriPripadnost(object sender, DirectEventArgs e)
        {
            List<int> selectedItems = e.GetIdList<int>("selected", "LdapPraviloID");
            List<string> selectedItemsNames = e.GetIdList<string>("selected", "Ime");
            string msg = "";

            for (var i = 0; i < selectedItems.Count; i++)
            {
                var clani = new EntityCollection<LdapPraviloEntitete>(new Filter<LdapPraviloEntitete>(selectedItems[i]));
                msg = String.Format("{0}{1}: {2} <br>", msg, selectedItemsNames[i], clani.Count());
            }

            if (!String.IsNullOrEmpty(msg))
            {
                msg = string.Format("<b>Izbrana pravila zajemajo naslednje število članov:</b><br><br>{0}", msg);
                ShowExtMessageBox("Pripadnost", msg);
            }
        }
        #endregion

        #region OdstraniZapise
        protected void OdstraniZapise(object sender, DirectEventArgs e)
        {
            List<int> selectedItems = e.GetIdList<int>("selected", "LdapPraviloID");
            List<string> selectedItemsNames = e.GetIdList<string>("selected", "Ime");
            int deleted = 0;

            for (var i = 0; i < selectedItems.Count; i++)
            {
                // Delete SIN_OBJKETI for Pravilo
                var sinObjketi = new EntityCollection<LdapSinObjekt>(
                    new Filter<LdapSinObjekt>().Where(r => r.LdapPraviloID == selectedItems[i]));
                sinObjketi.RemoveAll();
                sinObjketi.Update();

                // Delete pravilo
                LdapPravilo pravilo = EntityFactory.LoadEntity<LdapPravilo>(
                new Filter<LdapPravilo>().Where(r => r.LdapPraviloID == selectedItems[i]));
                pravilo.Delete();
                deleted++;
            }

            if (deleted > 0)
            {
                StorePravilaSeznam.DataBind();
                ShowExtNotification("Brisanje", String.Format("Brisanje je bilo uspešno. <br><br>Število izbrisanih zapisov: {0}.", deleted));
            }
        }
        #endregion
        #endregion        
        
    }
}
